package org.ns.longhttp.wrapper;

import com.alibaba.fastjson.JSON;
import org.ns.longhttp.selector.datamapper.DataResponseEntry;

import java.util.List;

public class GetResponseWrapper {

    private List<DataResponseEntry> data; // 所有的应答实体
    private long lastId; // 最后一个ID

    public GetResponseWrapper(List<DataResponseEntry> data) {
        setData(data);
    }

    public List<DataResponseEntry> getData() {
        return data;
    }

    public void setData(List<DataResponseEntry> data) {
        this.data = data;
        for (DataResponseEntry datum : data) {
            long id = datum.getId();
            if (id > lastId) {
                lastId = id;
            }
        }
    }

    public long getLastId() {
        return lastId;
    }

    public String toJson(){
        return toJson(null);
    }
    public String toJson(String callback) {
        String json = JSON.toJSONString(this);
        if (callback != null && !callback.isEmpty()) {
            return String.format("%s(%s)", callback, json);
        }
        return json;
    }
}
